package com.greenantmall.activity.fragment;

import it.sephiroth.android.library.widget.HListView;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.adapter.GoodsListAdapterForImageItem;
import com.greenantmall.adapter.GoodsListAdapterForTradeStyleLeft;
import com.greenantmall.bean.GoodsKind;
import com.greenantmall.bean.GoodsStyles;
import com.greenantmall.bean.ServerSection;
import com.greenantmall.bean.ShoppingCart;
import com.greenantmall.bean.StoreInfo;
import com.greenantmall.bean.UIGoods;
import com.greenantmall.bean.UIGoodsList;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.UtilTools;

public class TabFragmentStore extends Fragment {
	private View mViewContentLayout;
	private TextView mTvPrompt2;
	private TextView mTvPrompt3;
	private TextView mTvSwitch;
	private HListView mHLSecondKind;
	private ProgressBar mProgressBar;
	private TextView mTvPrompt;
	private TextView mTvTopOpenTimePrompt;
	private ViewPager mViewPager;
	private View mViewLayoutLeft;
	private View mViewLayoutRight;

	private List<GoodsKind> mGoodsKindList = new ArrayList<GoodsKind>();
	private List<GoodsStyles> mGoodsStylesList = new ArrayList<GoodsStyles>();
	private List<UIGoods> mDataList = new ArrayList<UIGoods>();
	private List<ShoppingCart> mShoppingCartList = new ArrayList<ShoppingCart>();
	private StoreInfo mCvsStoreInfo;
	
	private LeftLayoutManager mLeftLayoutManager;
	private RightLayoutManager mRightLayoutManager;
	private SecondKindHLAdapter mSecondKindHLAdapter;
	
	private boolean reloadDataFlag = false;
	private long flagLoadDataTimeGlobal = 0;
	
	private LayoutInflater mLayoutInflater;
	private int mTotalCount = 0;
	private boolean isLoadingData = false;
	private GoodsKind mCurrentGoodsKind;
	private GoodsStyles mCurrentGoodsStyles;
	
	private String mStrPromotion = "";
	private String mStrAll = "";
	
	private View lastSelectedView = null;
	private it.sephiroth.android.library.widget.AdapterView.OnItemClickListener mOnItemClickListener = 
			new it.sephiroth.android.library.widget.AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (isLoadingData) {
				return;
			}
			
			if(mCurrentGoodsStyles != null && mGoodsStylesList.get(arg2).getName().equals(mCurrentGoodsStyles.getName())) {
				return;
			}
			
			if(lastSelectedView != null) {
				TextView textView = (TextView)lastSelectedView.findViewById(R.id.tab_fragment_store_hlistview_item_name);
				textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_normal);
				textView.setTextColor(getResources().getColor(android.R.color.black));
			}
			
			lastSelectedView = arg1;
			TextView textView = (TextView)lastSelectedView.findViewById(R.id.tab_fragment_store_hlistview_item_name);
			textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_selected);
			textView.setTextColor(getResources().getColor(R.color.green));
			
			mCurrentGoodsStyles = mGoodsStylesList.get(arg2);
			mSecondKindHLAdapter.notifyDataSetChanged();
			
			flagLoadDataTimeGlobal = System.currentTimeMillis();
			retrieveGoodsList(0, 30, flagLoadDataTimeGlobal);
		}
	};

	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.equals(mTvSwitch)) {
				if (mViewPager.getCurrentItem() == 0) {
					mViewPager.setCurrentItem(1, false);
				} else {
					mViewPager.setCurrentItem(0, false);
				}
			} 
			else if(v.equals(mLeftLayoutManager.mViewForClick)
					|| v.equals(mRightLayoutManager.mViewForClick)) {
				if(mCurrentGoodsKind == null && mCurrentGoodsStyles == null) {
					loadData();
				} else {
					flagLoadDataTimeGlobal = System.currentTimeMillis();
					retrieveGoodsList(0, 30, flagLoadDataTimeGlobal);
					mProgressBar.setVisibility(View.VISIBLE);
					mTvPrompt.setVisibility(View.GONE);
				}
			}
		}
	};

	private PagerAdapter mPagerAdapter = new PagerAdapter() {

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			if (position == 0) {
				((ViewPager) container).removeView(mViewLayoutLeft);
			} else if (position == 1) {
				((ViewPager) container).removeView(mViewLayoutRight);
			}
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View layoutView = null;
			if (position == 0) {
				((ViewPager) container).addView(mViewLayoutLeft, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				layoutView = mViewLayoutLeft;
			} else if (position == 1) {
				((ViewPager) container).addView(mViewLayoutRight, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				layoutView = mViewLayoutRight;
			}

			return layoutView;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return 2;
		}
	};
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(Const.SHOPPING_CART_CHANGED)) {
				refreshShoppingCartList();
			}
		}
	};
	
	
	public void reloadData() {
		reloadDataFlag = true;
	}
	
	public void reloadData2() {
		isLoadingData = false;
		mTotalCount = 0;
		mCurrentGoodsKind = null;
		mCurrentGoodsStyles = null;
		mCvsStoreInfo = null;
		mGoodsKindList.clear();
		mGoodsStylesList.clear();
		mDataList.clear();
		mShoppingCartList.clear();
		mLeftLayoutManager.mListViewLeftAdapter.notifyDataSetInvalidated();
		mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetInvalidated();
		mRightLayoutManager.mAdapter.notifyDataSetInvalidated();
		mSecondKindHLAdapter.notifyDataSetInvalidated();
		mViewContentLayout.setVisibility(View.VISIBLE);
		mTvPrompt2.setVisibility(View.GONE);
		mTvPrompt3.setVisibility(View.GONE);
		this.loadData();
	}

	private void loadData() {
		flagLoadDataTimeGlobal = System.currentTimeMillis();
		mProgressBar.setVisibility(View.VISIBLE);
		mTvPrompt.setVisibility(View.GONE);

		// 读取当前ServerSection的StoreInfo list
		RetrieveDataAsyn.getInstance().getStoreInfosForStore(-1, new RetrieveDataListener() {
			long flagTime = flagLoadDataTimeGlobal;
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if(flagTime != flagLoadDataTimeGlobal) {
					return;
				}
				
				if(data != null) {
					mCvsStoreInfo = (StoreInfo)data;
				}
				
				if (mCvsStoreInfo == null) {
					// 没有获取到StoreInfo数据
					mProgressBar.setVisibility(View.GONE);
					mTvPrompt.setVisibility(View.VISIBLE);
					mTvPrompt.setText(R.string.goods_list_no_goods);
					
					mViewContentLayout.setVisibility(View.GONE);
					mTvPrompt2.setVisibility(View.VISIBLE);
					mTvPrompt3.setVisibility(View.GONE);
				} else {
					if(mCvsStoreInfo.isWaitForOpen()) {
						mViewContentLayout.setVisibility(View.GONE);
						mTvPrompt2.setVisibility(View.GONE);
						mTvPrompt3.setVisibility(View.VISIBLE);
					} else {
						// 获取到StoreInfo数据
						refreshShoppingCartList();
						// 请求GoodsKind list
						retrieveGoodsKindList(flagTime);
						// 设置营业
						String formatStr = getResources().getString(R.string.store_top_open_time_prompt);
						mTvTopOpenTimePrompt.setText(String.format(formatStr, 
								mCvsStoreInfo.getOpenTimeStr(), mCvsStoreInfo.getCloseTimeStr()));
					}
				}
			}
		});
	}
	
	private void retrieveGoodsKindList(final long flagLoadDataTime) {
		Log.d("CEXX", "CEXX => retrieveGoodsKindList");
		// 请求GoodsKind list
		mGoodsKindList.clear();
		mGoodsStylesList.clear();
		RetrieveDataAsyn.getInstance().getGoodsKind(mCvsStoreInfo.getStoreid(), -1, new RetrieveDataListener() {
			long flagTime = flagLoadDataTime;
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if (flagTime != flagLoadDataTimeGlobal) {
					return;
				}
				
				List<GoodsKind> goodsKindList = (List<GoodsKind>)data;
				if(goodsKindList != null && goodsKindList.size() > 0) {
					GoodsKind promtionGoodsKind = new GoodsKind();
					promtionGoodsKind.setName(mStrPromotion);
					mGoodsKindList.add(0, promtionGoodsKind);
					for(GoodsKind gk : goodsKindList) {
						if(!TextUtils.isEmpty(gk.getName())) {
							mGoodsKindList.add(gk);
						}
					}
					
					for(GoodsKind gk : mGoodsKindList) {
						List<GoodsStyles> gsList = gk.getGoodsStylesList();
						if(gsList == null) {
							gsList = new ArrayList<GoodsStyles>();
							gk.setGoodsStylesList(gsList);
						}
						GoodsStyles gs = new GoodsStyles();
						gs.setName(mStrAll);
						gsList.add(0, gs);
					}
					
					mCurrentGoodsKind = promtionGoodsKind;
					mGoodsStylesList.addAll(mCurrentGoodsKind.getGoodsStylesList());
					mCurrentGoodsStyles = mGoodsStylesList.get(0);
					mLeftLayoutManager.mListViewLeftAdapter.notifyDataSetChanged();
					mSecondKindHLAdapter.notifyDataSetChanged();
					
					mHLSecondKind.post(new Runnable() {
						@Override
						public void run() {
							if(lastSelectedView != null) {
								TextView textView = (TextView)lastSelectedView.findViewById(R.id.tab_fragment_store_hlistview_item_name);
								textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_normal);
								textView.setTextColor(getResources().getColor(android.R.color.black));
							}
							
							if(mHLSecondKind.getFirstVisiblePosition() == 0) {
								View first = mHLSecondKind.getChildAt(0);
								if(first != null) {
									TextView textView = (TextView)first.findViewById(R.id.tab_fragment_store_hlistview_item_name);
									textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_selected);
									textView.setTextColor(getResources().getColor(R.color.green));
									lastSelectedView = first;
								}
							}
						}
					});
					
					// 加载商品列表
					retrieveGoodsList(0, 30, flagTime);
				}
				else {
					// 没有获取到StoreInfo数据
					mProgressBar.setVisibility(View.GONE);
					mTvPrompt.setVisibility(View.VISIBLE);
					mTvPrompt.setText(R.string.goods_list_no_goods);
				}
			}
		});
	}
	
	private void retrieveGoodsList(int start, int expectCount, final long flagLoadDataTime) {
		if (isLoadingData) {
			return;
		}
		isLoadingData = true;
		
		List<String> storeIdList = new ArrayList<String>();
		storeIdList.add(mCvsStoreInfo.getStoreid());

		String kind = mCurrentGoodsKind.getName();
		if(kind.equals(mStrPromotion)) {
			kind = "";
		}
		
		String style = mCurrentGoodsStyles.getName();
		if(style.equals(mStrAll)) {
			style = "";
		}
		
		int requestId = start;
		RetrieveDataAsyn.getInstance().getGoodsListByKindAndStyle(kind, style, 
				start, expectCount, storeIdList, 2, requestId, new RetrieveDataListener() {
					long flagTime = flagLoadDataTime;
					String kindRecord = mCurrentGoodsKind.getName();
					String styleRecord = mCurrentGoodsStyles.getName();
					@Override
					public void dataRetrieved(int requestId, Object data) {
						if (flagTime != flagLoadDataTimeGlobal) {
							return;
						}
						
						if(!kindRecord.equals(mCurrentGoodsKind.getName()) 
								|| !styleRecord.equals(mCurrentGoodsStyles.getName())) {
							return;
						}
						
						if (data != null) {
							UIGoodsList uiGoodsList = (UIGoodsList) data;

							if (requestId == 0) {
								mDataList.clear();
								mTotalCount = uiGoodsList.getTotalNum();
								Log.d("CEXX", "GoodsListTradeStyleActivity => dataRetrieved => mTotalCount : " + mTotalCount);
								mDataList.addAll(uiGoodsList.getUIGoodsList());
								
								if(kindRecord.equals(mStrPromotion)) {
									mRightLayoutManager.mAdapter.setDisplayMarketPrice(true);
									mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(true);
								}
								else{
									mRightLayoutManager.mAdapter.setDisplayMarketPrice(false);
									mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(false);
								}
								
								mRightLayoutManager.mAdapter.notifyDataSetInvalidated();
								mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetInvalidated();
							} else {
								mDataList.addAll(uiGoodsList.getUIGoodsList());
								
								if(kindRecord.equals(mStrPromotion)) {
									mRightLayoutManager.mAdapter.setDisplayMarketPrice(true);
									mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(true);
								}
								else{
									mRightLayoutManager.mAdapter.setDisplayMarketPrice(false);
									mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.setDisplayMarketPrice(false);
								}
								
								mRightLayoutManager.mAdapter.notifyDataSetChanged();
								mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
							}

							isLoadingData = false;
							if (mDataList.size() == 0) {
								mProgressBar.setVisibility(View.GONE);
								mTvPrompt.setVisibility(View.VISIBLE);
								mTvPrompt.setText(R.string.goods_list_no_goods);
							} else {
								mProgressBar.setVisibility(View.GONE);
								mTvPrompt.setVisibility(View.GONE);
							}
						}
					}
				});
		mProgressBar.setVisibility(View.VISIBLE);
		mTvPrompt.setVisibility(View.GONE);
	}

	private void refreshShoppingCartList() {
		mShoppingCartList.clear();
		
		List<ShoppingCart> scList = UtilTools.getShoppingCartDatas();
		if (scList != null) {
			mShoppingCartList.addAll(scList);
		}

		if (mLeftLayoutManager != null && mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft != null) {
			mLeftLayoutManager.mGoodsListAdapterForTradeStyleLeft.notifyDataSetChanged();
		}

		if (mRightLayoutManager != null && mRightLayoutManager.mAdapter != null) {
			mRightLayoutManager.mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentStore => onCreate");
		mStrPromotion = getResources().getString(R.string.goods_kind_promotion);
		mStrAll = getResources().getString(R.string.goods_kind_goods_style_all);
		
		IntentFilter filter = new IntentFilter(Const.SHOPPING_CART_CHANGED);
		getActivity().registerReceiver(mReceiver, filter);
	}

	@Override
	public void onResume() {
		if(reloadDataFlag) {
			reloadDataFlag = false;
			reloadData2();
		}
		super.onResume();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d("CEXX", "CEXX => TabFragmentStore => onCreateView");
		return inflater.inflate(R.layout.tab_fragment_store, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentStore => onViewCreated");

		mLayoutInflater = LayoutInflater.from(getActivity());
		mViewLayoutLeft = mLayoutInflater.inflate(R.layout.tab_fragment_store_left, null);
		mViewLayoutRight = mLayoutInflater.inflate(R.layout.tab_fragment_store_right, null);

		mViewContentLayout = view.findViewById(R.id.tab_fragmen_store_content_layout);
		mTvPrompt2 = (TextView) view.findViewById(R.id.tab_fragment_store_tv_prompt_2);
		mTvPrompt3 = (TextView) view.findViewById(R.id.tab_fragment_store_tv_prompt_3);
		mTvSwitch = (TextView) view.findViewById(R.id.tab_fragment_store_switch);
		mHLSecondKind = (HListView) view.findViewById(R.id.tab_fragment_store_second_kind_hl);
		mViewPager = (ViewPager) view.findViewById(R.id.tab_fragment_store_view_pager);
		mProgressBar = (ProgressBar) view.findViewById(R.id.tab_fragment_store_prgressbar);
		mTvPrompt = (TextView) view.findViewById(R.id.tab_fragment_store_tv_prompt);
		mTvTopOpenTimePrompt = (TextView) view.findViewById(R.id.tab_fragment_store_top_open_time_tv);

		mLeftLayoutManager = new LeftLayoutManager();
		mRightLayoutManager = new RightLayoutManager();
		mSecondKindHLAdapter = new SecondKindHLAdapter();
		
		mHLSecondKind.setSelector(android.R.color.transparent);
		mHLSecondKind.setOnItemClickListener(mOnItemClickListener);
		mHLSecondKind.setAdapter(mSecondKindHLAdapter);
		mTvSwitch.setOnClickListener(mOnClickListener);
		mViewPager.setAdapter(mPagerAdapter);
		
//		// ViewPager禁止滑动
//		mViewPager.setOnTouchListener(new OnTouchListener() {
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				return false;
//			}
//		});
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentStore => onActivityCreated");
		mViewContentLayout.setVisibility(View.VISIBLE);
		mTvPrompt2.setVisibility(View.GONE);
		mTvPrompt3.setVisibility(View.GONE);
		this.loadData();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("CEXX", "CEXX => TabFragmentStore => onDestroy");
		getActivity().unregisterReceiver(mReceiver);
	}
	
	private class SecondKindHLAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (mGoodsStylesList != null) {
				return mGoodsStylesList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View child = convertView;
			ViewHolder holder = null;
			if (child == null) {
				child = mLayoutInflater.inflate(R.layout.tab_fragment_store_hlistview_item, null);
				holder = new ViewHolder(child);
				child.setTag(holder);
			} else {
				holder = (ViewHolder) child.getTag();
			}

			GoodsStyles goodsStyles = mGoodsStylesList.get(position);
			holder.getTvName().setText(goodsStyles.getName());
			if(mCurrentGoodsStyles != null && goodsStyles.getName().equals(mCurrentGoodsStyles.getName())) {
				holder.getTvName().setBackgroundResource(R.drawable.tab_fragment_store_bg_selected);
				holder.getTvName().setTextColor(getResources().getColor(R.color.green));
			}
			else {
				holder.getTvName().setBackgroundResource(R.drawable.tab_fragment_store_bg_normal);
				holder.getTvName().setTextColor(getResources().getColor(android.R.color.black));
			}

			return child;
		}

		private class ViewHolder {
			private View mContentView;
			private TextView mTvName;

			public ViewHolder(View contentView) {
				mContentView = contentView;
			}

			public TextView getTvName() {
				if (mTvName == null) {
					mTvName = (TextView) mContentView.findViewById(R.id.tab_fragment_store_hlistview_item_name);
				}
				return mTvName;
			}
		}
	}
	
	private class LeftLayoutManager implements OnItemClickListener, OnScrollListener {
		private View mViewForClick;
		private ListView mListViewLeft;
		private GridView mGridViewLeft;
		private ListViewLeftAdapter mListViewLeftAdapter;
		private GoodsListAdapterForTradeStyleLeft mGoodsListAdapterForTradeStyleLeft;

		public LeftLayoutManager() {
			mViewForClick = mViewLayoutLeft.findViewById(R.id.tab_fragment_store_left_for_click);
			mListViewLeft = (ListView) mViewLayoutLeft.findViewById(R.id.tab_fragment_store_left_listview);
			mListViewLeft.setOnItemClickListener(this);
			mListViewLeftAdapter = new ListViewLeftAdapter();
			mListViewLeft.setAdapter(mListViewLeftAdapter);

			mGridViewLeft = (GridView) mViewLayoutLeft.findViewById(R.id.tab_fragment_store_left_gridview);
			mGridViewLeft.setOnItemClickListener(this);
			mGoodsListAdapterForTradeStyleLeft = new GoodsListAdapterForTradeStyleLeft(getActivity(), mDataList,
					mShoppingCartList);
			mGridViewLeft.setAdapter(mGoodsListAdapterForTradeStyleLeft);
			mGridViewLeft.setOnScrollListener(this);
			mViewForClick.setOnClickListener(mOnClickListener);
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (parent.equals(mListViewLeft)) {
				if (isLoadingData) {
					return;
				}
				
				if(mCurrentGoodsKind != null && mGoodsKindList.get(position).getName().equals(mCurrentGoodsKind.getName())) {
					return;
				}
				
				mCurrentGoodsKind = mGoodsKindList.get(position);
				mListViewLeftAdapter.notifyDataSetChanged();
				
				mGoodsStylesList.clear();
				List<GoodsStyles> styleList = mCurrentGoodsKind.getGoodsStylesList();
				if(styleList != null) {
					for(GoodsStyles style : styleList) {
						if(!TextUtils.isEmpty(style.getName())) {
							mGoodsStylesList.add(style);
						}
					}
				}
				
				mCurrentGoodsStyles = mGoodsStylesList.get(0);
				mSecondKindHLAdapter.notifyDataSetInvalidated();
				
				mHLSecondKind.post(new Runnable() {
					@Override
					public void run() {
						if(lastSelectedView != null) {
							TextView textView = (TextView)lastSelectedView.findViewById(R.id.tab_fragment_store_hlistview_item_name);
							textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_normal);
							textView.setTextColor(getResources().getColor(android.R.color.black));
						}
						
						if(mHLSecondKind.getFirstVisiblePosition() == 0) {
							View first = mHLSecondKind.getChildAt(0);
							if(first != null) {
								TextView textView = (TextView)first.findViewById(R.id.tab_fragment_store_hlistview_item_name);
								textView.setBackgroundResource(R.drawable.tab_fragment_store_bg_selected);
								textView.setTextColor(getResources().getColor(R.color.green));
								lastSelectedView = first;
							}
						}
					}
				});
				
				flagLoadDataTimeGlobal = System.currentTimeMillis();
				retrieveGoodsList(0, 30, flagLoadDataTimeGlobal);
				
				mProgressBar.setVisibility(View.VISIBLE);
				mTvPrompt.setVisibility(View.GONE);
			} else if (parent.equals(mGridViewLeft)) {
				Log.d("CEXX", "CEXX ---> GoodsListActivity ---> onItemClick ---> position : " + position);
//				UIGoods uiGoods = mDataList.get(position);
//				Intent intent = new Intent(getActivity(), DetailActivity.class);
//				intent.putExtra(DetailActivity.KEY_DETAIL_OBJECTID, uiGoods.getObjectId());
//				getActivity().startActivity(intent);
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			if (totalItemCount >= mTotalCount) {
				return;
			}

			if (isLoadingData) {
				return;
			}

			if (firstVisibleItem + visibleItemCount < totalItemCount) {
				return;
			}

			int start = totalItemCount;
			int expectedCount = 18;
			flagLoadDataTimeGlobal = System.currentTimeMillis();
			retrieveGoodsList(start, expectedCount, flagLoadDataTimeGlobal);
		}

		public class ListViewLeftAdapter extends BaseAdapter {

			@Override
			public int getCount() {
				if (mGoodsKindList != null) {
					return mGoodsKindList.size();
				}
				return 0;
			}

			@Override
			public Object getItem(int arg0) {
				return arg0;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View child = convertView;
				ViewHolder holder = null;
				if (child == null) {
					child = mLayoutInflater.inflate(R.layout.tab_fragment_store_left_listview_item, null);
					holder = new ViewHolder(child);
					child.setTag(holder);
				} else {
					holder = (ViewHolder) child.getTag();
				}

				GoodsKind goodsKind = mGoodsKindList.get(position);
				holder.getTvName().setText(goodsKind.getName());
				if (mCurrentGoodsKind != null && goodsKind.getName().equals(mCurrentGoodsKind.getName())) {
					holder.getViewLine().setBackgroundResource(R.color.green);
					holder.getTvName().setTextColor(getResources().getColor(R.color.green));
				} else {
					holder.getViewLine().setBackgroundResource(android.R.color.transparent);
					holder.getTvName().setTextColor(getResources().getColor(android.R.color.black));
				}
				
				if(mStrPromotion.equals(goodsKind.getName())) {
					holder.getViewStar().setVisibility(View.VISIBLE);
				}
				else {
					holder.getViewStar().setVisibility(View.GONE);
				}

				return child;
			}

			private class ViewHolder {
				private View mContentView;
				private TextView mTvName;
				private View mViewStar;
				private View mViewLine;

				public ViewHolder(View contentView) {
					mContentView = contentView;
				}

				public TextView getTvName() {
					if (mTvName == null) {
						mTvName = (TextView) mContentView.findViewById(R.id.tab_fragment_store_left_listview_item_name);
					}
					return mTvName;
				}
				
				public View getViewStar() {
					if (mViewStar == null) {
						mViewStar = mContentView.findViewById(R.id.tab_fragment_store_left_listview_item_star);
					}
					return mViewStar;
				}

				public View getViewLine() {
					if (mViewLine == null) {
						mViewLine = mContentView.findViewById(R.id.tab_fragment_store_left_listview_item_line);
					}
					return mViewLine;
				}
			}
		}
	}

	private class RightLayoutManager implements OnItemClickListener, OnScrollListener {
		private View mViewForClick;
		private GridView mGridViewRight;
		private GoodsListAdapterForImageItem mAdapter;

		public RightLayoutManager() {
			mViewForClick = mViewLayoutRight.findViewById(R.id.tab_fragment_store_right_for_click);
			mGridViewRight = (GridView) mViewLayoutRight.findViewById(R.id.tab_fragment_store_right_gridview);
			mAdapter = new GoodsListAdapterForImageItem(getActivity(), mDataList, mShoppingCartList);
			mGridViewRight.setAdapter(mAdapter);
			mGridViewRight.setOnScrollListener(this);
			mGridViewRight.setOnItemClickListener(this);
			mViewForClick.setOnClickListener(mOnClickListener);
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Log.d("CEXX", "CEXX ---> GoodsListActivity ---> onItemClick ---> position : " + position);
//			UIGoods uiGoods = mDataList.get(position);
//			Intent intent = new Intent(getActivity(), DetailActivity.class);
//			intent.putExtra(DetailActivity.KEY_DETAIL_OBJECTID, uiGoods.getObjectId());
//			getActivity().startActivity(intent);
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			if (totalItemCount >= mTotalCount) {
				return;
			}

			if (isLoadingData) {
				return;
			}

			if (firstVisibleItem + visibleItemCount < totalItemCount) {
				return;
			}

			int start = totalItemCount;
			int expectedCount = 18;
			flagLoadDataTimeGlobal = System.currentTimeMillis();
			retrieveGoodsList(start, expectedCount, flagLoadDataTimeGlobal);
		}

	}
}
